package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceimpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /*
    * 插入菜品数据
    * */
    @Transactional  //事务注解
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //获取刚插入的id值
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(e->{
                e.setDishId(id);
            });
            dishFlavorsMapper.insertFlavors(flavors);
        }

    }

    @Override
    public PageResult pageDish(DishPageQueryDTO dishPageQueryDTO) {
         PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
         Page<DishVO> page=dishMapper.pageSelect(dishPageQueryDTO);
         return new PageResult(page.getTotal(),page.getResult());
    }

    /*
    * 菜品批量删除
    * */
    @Override
    @Transactional
    public void delectDish(List<Long> ids) {
        //先判断菜品是否能够删除，即是否起售中
        ids.forEach(id->{
            Dish dish=dishMapper.getById(id);
            if (dish.getStatus()== StatusConstant.ENABLE){
                //当前菜品处于起售中，
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //判断是否与套餐关联
        List<Long> setmealByDishId = setmealDishMapper.getSetmealByDishId(ids);
        if (setmealByDishId.size()!=0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品和与其关联的口味数据

           dishMapper.deleteById(ids);
           dishFlavorsMapper.deleteById(ids);

    }

    /**
     * 根据id查询菜品
     * */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);
        List<DishFlavor> dishFlavors=dishFlavorsMapper.getById(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * */
    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
        //修改菜品基本信息
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);
        //删除原本的口味，再添加新的口味
        List<Long> ids=new ArrayList<>();
        ids.add(dishDTO.getId());
        dishFlavorsMapper.deleteById(ids);
        //更新数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(e->{
                e.setDishId(dishDTO.getId());
            });
            dishFlavorsMapper.insertFlavors(flavors);
        }

    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId) {

        List<Dish> dishes=dishMapper.getByCategoryId(categoryId);
        return dishes;
    }
}
